SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
Create Procedure [dbo].[SP_TMSABA_MENS_I04]
/*-------------------------------------------------------*/
/*---Empresa              : OFISIS S.A.                --*/
/*---Cliente              : OFISIS                 --*/
/*---Sistema              : Tesoreria  x               --*/
/*---M¸dulo               : Empresa                    --*/
/*---Programa             : Apertura Cuenta Bancaria   --*/
/*---			    Individual (Carga)	       --*/
/*---                       Bancos                     --*/
/*---Script               : tmsaba_m.sql               --*/
/*---Nombre SP            : SP_TMSABA_MENS_I04         --*/
/*---Desarrollado por     : Javier Flores A.           --*/
/*---Fecha Creaci¸n       : 15/07/1999                 --*/
/*---Base Datos           : Microsoft Sql Server       --*/
/*---Versi¸n              : 7.0                        --*/
/*---Invoca a SP          :                            --*/
/*-------------------------------------------------------*/

/*-------------------------------------------------------*/
/*---Modificado 1 por     :                            --*/
/*---Fecha Modificaci¸n   :                            --*/ 
/*---Detalle Modificaci¸n :                            --*/
/*-------------------------------------------------------*/

/*-------------------------------------------------------*/
/*---Comentarios                                       --*/
/*---                                                  --*/
/*---                                                  --*/
/*-------------------------------------------------------*/

/*-------------------------------------------------------*/
/*---Drop Proc SP_TMSABA_MENS_I04                      --*/
/*---Grant all on SP_TMSABA_MENS_I04 to public         --*/ 
/*---SP_TCMOVI_BANC_I04                                --*/
/*-------------------------------------------------------*/
 @ISCO_EMPR TD_VC_002, 
 @ISCO_USUA_MODI TD_VC_008_USUA,
 @ISCO_BANC TD_VC_003, 
 @ISNU_CNTA_BANC TD_VC_020
As
Declare

   @VSAA_BNCO       TD_VC_004,
   @VSMM_BNCO       TD_VC_002,
   @VSAA_BNCO_ANTE  TD_VC_004,
   @VSMM_BNCO_ANTE  TD_VC_002,
   @VSTI_SITU       TD_VC_003,
   @CSAA_BNCO       TD_VC_004, 
   @CSMM_BNCO       TD_VC_002, 
   @CNIM_SALD_INIC  TD_NU_016_004, 
   @CNIM_INGR       TD_NU_016_004, 
   @CNIM_EGRE       TD_NU_016_004, 
   @CNUL_CORR_INGR  TD_IN_001, 
   @CNUL_CORR_EGRE  TD_IN_001, 
   @CNUL_CORR_INEG  TD_IN_001,
   @CSTI_SITU       TD_VC_003,
   @CSCO_USUA_CREA  TD_VC_008, 
   @CSCO_USUA_MODI  TD_VC_008,
   @CDFE_USUA_CREA  TD_DT_001,
   @CDFE_USUA_MODI  TD_DT_001,
   @VNIM_SALD_INIC  TD_NU_016_004

    Create Table #TWSABA_MENS
   ( AA_BNCO  char(4), 
     MM_BNCO  char(2), 
     IM_SALD_INIC numeric(16,4),
     IM_INGR  numeric(16,4), 
     IM_EGRE  numeric(16,4), 
     UL_CORR_INGR int , 
     UL_CORR_EGRE int, 
     UL_CORR_INEG int , 
     TI_SITU  char(3),
     CO_USUA_CREA VARCHAR(8),
     CO_USUA_MODI VARCHAR(8),
     FE_USUA_CREA DATETIME,
     FE_USUA_MODI DATETIME,	
   )

 Select @VSAA_BNCO = Max(AA_BNCO), @VSMM_BNCO = MAX(MM_BNCO)
 From TMSABA_MENS
 Where CO_EMPR = @ISCO_EMPR
 And   CO_BANC = @ISCO_BANC
 And   NU_CNTA_BANC = @ISNU_CNTA_BANC
 And   TI_SITU = 'ACT'

 If @VSMM_BNCO = '01'
   Begin
     Select @VSMM_BNCO_ANTE = '12'
     Select @VSAA_BNCO_ANTE = @VSAA_BNCO - 1
   End
 Else
   Begin
     Select @VSMM_BNCO_ANTE = @VSMM_BNCO - 1
     Select @VSAA_BNCO_ANTE = @VSAA_BNCO
   End

 If Len(@VSMM_BNCO_ANTE) = 1 
    Select @VSMM_BNCO_ANTE = '0'+@VSMM_BNCO_ANTE

 Select @VNIM_SALD_INIC = Isnull(IM_SALD_INIC,0)+ Isnull(IM_INGR,0) - Isnull(IM_EGRE,0)
 From   TMSABA_MENS
 Where  CO_EMPR = @ISCO_EMPR
 And    CO_BANC = @ISCO_BANC
 And    NU_CNTA_BANC = @ISNU_CNTA_BANC
 And    AA_BNCO = @VSAA_BNCO_ANTE
 And    MM_BNCO = @VSMM_BNCO_ANTE

 Declare CU_TMSABA_MENS_I04_1 Cursor
 For
 Select AA_BNCO, MM_BNCO, IM_SALD_INIC, IM_INGR, IM_EGRE, UL_CORR_INGR,
        UL_CORR_EGRE, UL_CORR_INEG, TI_SITU, CO_USUA_CREA, FE_USUA_CREA, 
	CO_USUA_MODI, FE_USUA_MODI
 From   TMSABA_MENS
 Where  CO_EMPR = @ISCO_EMPR
 And    CO_BANC = @ISCO_BANC
 And    NU_CNTA_BANC = @ISNU_CNTA_BANC

 Open CU_TMSABA_MENS_I04_1
 Fetch CU_TMSABA_MENS_I04_1 INTO @CSAA_BNCO, @CSMM_BNCO, @CNIM_SALD_INIC, @CNIM_INGR, 
  @CNIM_EGRE, @CNUL_CORR_INGR, @CNUL_CORR_EGRE, @CNUL_CORR_INEG, @CSTI_SITU, 
  @CSCO_USUA_CREA, @CDFE_USUA_CREA, @CSCO_USUA_MODI, @CDFE_USUA_MODI 

 While (@@FETCH_STATUS = 0)
   Begin
     If (@@FETCH_STATUS < 0)
        Begin
          RAISERROR 20001 'FIN DE ARCHIVO'
          Close CU_TMSABA_MENS_I04_1
          Deallocate CU_TMSABA_MENS_I04_1
          Return
        End

     If @VSAA_BNCO = @CSAA_BNCO and @VSMM_BNCO = @CSMM_BNCO
        Select @CNIM_SALD_INIC = ISNULL(@VNIM_SALD_INIC,0)

     Insert into #TWSABA_MENS VALUES (@CSAA_BNCO, @CSMM_BNCO, @CNIM_SALD_INIC, @CNIM_INGR, 
                                        @CNIM_EGRE, @CNUL_CORR_INGR, @CNUL_CORR_EGRE, @CNUL_CORR_INEG, @CSTI_SITU,
					@CSCO_USUA_CREA, @CSCO_USUA_MODI, @CDFE_USUA_CREA, @CDFE_USUA_MODI )

     Fetch CU_TMSABA_MENS_I04_1 INTO @CSAA_BNCO, @CSMM_BNCO, @CNIM_SALD_INIC, @CNIM_INGR, 
                                   @CNIM_EGRE, @CNUL_CORR_INGR, @CNUL_CORR_EGRE, @CNUL_CORR_INEG, @CSTI_SITU,
				     @CSCO_USUA_CREA, @CDFE_USUA_CREA, @CSCO_USUA_MODI, @CDFE_USUA_MODI 
      
   End

  Close CU_TMSABA_MENS_I04_1
  Deallocate CU_TMSABA_MENS_I04_1

Select AA_BNCO, MM_BNCO, ISNULL(IM_SALD_INIC, 0), ISNULL(IM_INGR, 0), ISNULL(IM_EGRE, 0), 
       UL_CORR_INGR, UL_CORR_EGRE, UL_CORR_INEG, TI_SITU, CO_USUA_CREA, CO_USUA_MODI, FE_USUA_CREA,  
       FE_USUA_MODI	
From #TWSABA_MENS

Return

/*------------------------------ FIN ---------------------------------*/


GO
